from django.core.management.base import BaseCommand, CommandError
from mysqldb.models import PayAdminUserList, PayAdminGroupList


def initialization():
    # 管理组
    PayAdminGroupList.objects.update_or_create(defaults={"name": "超级管理员"}, id=1)
    PayAdminGroupList.objects.update_or_create(defaults={"name": "临时管理员"}, id=2)
    # 管理员
    PayAdminUserList.objects.update_or_create(
        defaults={
            "username": "admin",
            "phone": "13896370136",
            "email": "3361098@qq.com",
            "nicename": "超级管理员",
            "gender": 1,
            "status": 1,
            "group": 1,
            "is_super": 1,
        },
        id=1,
    )


class Command(BaseCommand):
    help = "My custom startup command"

    def handle(self, *args, **kwargs):
        try:
            # put startup code here
            print("# [\033[1;35m 加载初始化数据 \033[0m] ...")
            # 清除缓存
            # clear_cache()
            # 启动脚本
            initialization()
        except BaseException as err:
            print(err)
            raise CommandError("Initalization failed.")
